package com.cz.video.website.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.video.common.constant.CacheNames;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.exception.ServiceException;
import com.cz.video.common.utils.StringUtils;
import com.cz.video.website.domain.WebsiteMediaCategory;
import com.cz.video.website.domain.bo.WebsiteMediaCategoryBo;
import com.cz.video.website.domain.vo.WebsiteMediaCategoryVo;
import com.cz.video.website.mapper.WebsiteMediaCategoryMapper;
import com.cz.video.website.service.IWebsiteMediaCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 电影分类表 服务实现类
 * </p>
 *
 * @author 洪圆胜
 * @since 2023-10-15
 */
@Service
@RequiredArgsConstructor
public class WebsiteMediaCategoryServiceImpl extends ServiceImpl<WebsiteMediaCategoryMapper, WebsiteMediaCategory> implements IWebsiteMediaCategoryService {

    private final WebsiteMediaCategoryMapper websiteMediaCategoryMapper;

    /**
     *新增电影分类
     * @param websiteMediaCategory
     * @return
     */
    @Override
    public int addMediaCategory(WebsiteMediaCategoryBo websiteMediaCategory) {
        checkNameUnique(websiteMediaCategory);
        int rows = websiteMediaCategoryMapper.insert(websiteMediaCategory);
        return rows;
    }

    @Override
    public int update(WebsiteMediaCategoryBo websiteMediaCategory) {
        checkNameUnique(websiteMediaCategory);
        return websiteMediaCategoryMapper.updateById(websiteMediaCategory);
    }

    @Override
    public TableDataInfo<WebsiteMediaCategoryVo> page(WebsiteMediaCategoryBo websiteMediaCategory, PageQuery pageQuery) {
        Page<WebsiteMediaCategoryVo> page=websiteMediaCategoryMapper.selectPageList(buildQueryWrapper(websiteMediaCategory), pageQuery.build());
        return TableDataInfo.build(page);
    }

    /**
     * 检查名称是否唯一
     * @param mediaCategoryBo
     */
    private void checkNameUnique(WebsiteMediaCategoryBo mediaCategoryBo) {
        boolean exists = websiteMediaCategoryMapper.exists(Wrappers.<WebsiteMediaCategory>lambdaQuery()
            .eq(mediaCategoryBo.getCategoryId() != null, WebsiteMediaCategory::getCategoryId, mediaCategoryBo.getCategoryId())
            .eq(WebsiteMediaCategory::getName, mediaCategoryBo.getName()));
        if (exists) {
            throw new ServiceException("分类名不能重复");
        }
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.WEBSITE_MOVIE, key = "#id")
    public int removeById(Long id) {
        return websiteMediaCategoryMapper.deleteById(id);
    }

    @Override
    @Cacheable(cacheNames = CacheNames.WEBSITE_MOVIE, key = "#id")
    public WebsiteMediaCategoryVo getById(Long id) {
        return websiteMediaCategoryMapper.selectDetailInfo(id);
    }

    private Wrapper<WebsiteMediaCategory> buildQueryWrapper(WebsiteMediaCategoryBo websiteMediaCategoryBo) {
        Map<String, Object> params = websiteMediaCategoryBo.getParams();
        QueryWrapper<WebsiteMediaCategory> wrapper = Wrappers.query();
        wrapper
//            .eq("wmc.del_flag", CommonConstants.NOT_DELETED)
            .eq("1", 1)
            .eq(ObjectUtil.isNotNull(websiteMediaCategoryBo.getCategoryId()), "wmc.category_id", websiteMediaCategoryBo.getCategoryId())
            .like(StringUtils.isNotBlank(websiteMediaCategoryBo.getName()), "wmc.name", websiteMediaCategoryBo.getName())
            .eq(ObjectUtil.isNotNull(websiteMediaCategoryBo.getStatus()), "wmc.status", websiteMediaCategoryBo.getStatus())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "wmc.create_time", params.get("beginTime"), params.get("endTime"))
        ;
        return wrapper;
    }

}
